<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController as Controller;
use Crypt;
use Illuminate\Http\Request;
use App\Model\User;
use App\Model\{Admin,Config,Money};
use Illuminate\Support\Facades\DB;

/**
 * @menu_cat 资金管理
 * @icon asterisk
 */
class MoneyController extends Controller{
	
	public function __invoke(){
        return $this->index(func_get_args());
    }
	
	/**
	 * @menu 合伙人管理收入
	 * @return [type] [description]
	 */
	public function partner_income(){
		$list = DB::table('partner_income');
		$this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
        $uids = get_array_keys($list->items(),'uid');

        $field = ['id','nickname'];
        $users = DB::table('userdata')->select($field)->whereIn('id',$uids)->get()->toArray();
        #dd($users);
        $users = arrays_set_key($users,'id');

        foreach($list->items() as &$item){
        	$item->user = $users[$item->uid];
            $item->checkout_day = date('Ymd',strtotime("+2 day",$item->add_time));
        }
        
        return view('Admin.Money.partner_income')->with([
            'list' => $list,
            '_search'=>$search,
            'status'=>Config::get_config_bykey('partner_income_status'),
        ]);
    }

    /**
     * 
     */
    public function ajax_checkout($id){
        $rs = Money::ManagementRevenueSettlement($id);

        return back();
    }

    /**
     * @menu 合伙人资金明细
     */
    public function partner_moneylog(){
    	$list = DB::table('partner_moneylog');
		$this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
        $uids = get_array_keys($list->items(),'uid');

        $field = ['id','nickname'];
        $users = DB::table('userdata')->select($field)->whereIn('id',$uids)->get()->toArray();
        #dd($users);
        $users = arrays_set_key($users,'id');

        foreach($list->items() as &$item){
        	$item->user = $users[$item->uid];
        }
        
        return view('Admin.Money.partner_moneylog')->with([
            'list' => $list,
            '_search'=>$search,
            'type'=>Config::get_config_bykey('partner_moneylog_type'),
        ]);
    }

    /**
     * @menu 用户资金明细
     */
    public function user_moneylog()
    {
        
        $param = $this->getSearchParams();
        if(isset($param['uid']) && $param['uid']>0)
        {
            $suffix = substr($param['uid'], -2);
            if($suffix<10) {
                $suffix = str_pad($suffix, 2, "0", STR_PAD_LEFT);
            }
            $table = 'refererdata_'.$suffix;
            
            $list = DB::connection('db3')->table($table)->orderBy('id','desc');
            $this->_search($list, $search);
            $list = $list->paginate(20);

            foreach($list->items() as &$item) {
                $item->income_type_text = $this->text($item->type, $item->income_type, $item);
            }
        }
        
        return view('Admin.Money.user_moneylog')->with([
            'list' => isset($list)?$list:null,
            '_search'=>$search,
        ]);
    }

    private function text($type, $income_type, $item) {
        $text = [
            1=>[
                0=>'徒弟ID: {td_id} 收入分成',
                1=>"徒弟ID: {td_id} 第{identifying}次奖励发放",
                2=>"唤醒徒弟奖励",
                3=>"瓜分五元红包奖励",
            ],
            2=>[
                1 => '分享文章被阅读奖励',
                2 => '新用户首次登录奖励',
                3 => '签到奖励',
                6 => [1 => '首次绑定提现账号奖励', 2 => '首次阅读资讯奖励', 3 => '首次完善个人资料奖励', 14 => '首次阅读时段红包资讯奖励', 15 => '阅读时段红包资讯奖励', 16 => '每日发表评论奖励', 17 => '每日分享朋友圈奖励', 18 => '每日分享微信群奖励', 20 => '每日观看视频奖励', 21 => '首次成功邀请好友奖励', 22 => '成功填写邀请码', 23 => '玩游戏赚钱', 24 => '阅读小说点广告', 25 => '回归奖励',],
                7 => [0 => '阅读资讯奖励', 1 => '观看视频奖励'],
                10 => '连续登录奖励',
                11 => '瓜分5元红包',
                12 => '随机红包奖励',
                13 => [1 => '收徒活动奖励'],
            ]
        ];
        if(!is_array($text[$type][$income_type])) {
            $txt = preg_replace_callback('#\{(.*?)\}#i', function($matchs) use ($item){
                return $item->{$matchs[1]};
            }, $text[$type][$income_type]);
            return $txt;
        }
        else if( is_array($text[$type][$income_type]) ) {
            switch ($income_type) {
                case '6':
                    return $text[$type][$income_type][$item->task_id];
                    break;
                case '7':
                    return $text[$type][$income_type][$item->identifying];
                    break;
                case '13':
                    return $text[$type][$income_type][$item->identifying];
                    break;    
                default:
                    return $text[$type][$income_type];
                    break;
            }
        }
    }
}